<template>
    <div>
        <div style="width:900px; color: #e8eff9;border: 1px solid #e9ebed;margin: 50px auto; padding: 0">
            <div style="width: 100%; color: #464d5b;background: #e9ebed; height: 35px; line-height: 35px; padding-left: 10px; margin-bottom: 20px;">
                控制命令下发
            </div>
            <a-form :form="form" @keydown.native.enter.prevent='confirm' class="modelSearch">
            <a-form-item v-bind="formStyle" label="模块名称" required>
                <a-select :getPopupContainer="trigger => trigger.parentNode" @change="getCommandListAll"
                          placeholder="请选择模块名称" v-model="sendParams.tid">
                    <a-select-option v-for="item in toolListAll" :value="item.tid" :key="item.tid">{{ item.name }}
                    </a-select-option>
                </a-select>
            </a-form-item>
            <a-form-item v-bind="formStyle" label="指令类型" required>
                <a-select placeholder="请选择指令类型" v-model="functionType">
                    <a-select-option v-for="item in functionTypeList" :key="item.value" :value="item.value">{{item.label}}
                    </a-select-option>
                </a-select>
            </a-form-item>
             <a-form-item v-bind="formStyle" label="检测项名称" required>
                <a-select
                        v-model="checkId" allowClear placeholder="请选择检测项名称"
                        :getPopupContainer="trigger => trigger.parentNode">
                        <a-select-option v-for="item in checkItemALL" :key="item.value" :value="item.value">{{ item.name }}</a-select-option>
                    </a-select>
            </a-form-item>
            <a-form-item v-bind="formStyle" label="功能" required>
                <a-select @change="getSendParamsObjFun"
                          v-model="sendParams.cmd_name" allowClear placeholder="请选择功能"
                          :getPopupContainer="trigger => trigger.parentNode">
                        <a-select-option v-for="item in belongCommandListAll" :key="item.cmd_name"
                                         :value="item.cmd_name">{{ item.name }}
                        </a-select-option>
                    </a-select>
            </a-form-item>
                    <a-form-item v-bind="formStyle" :label="pramsObj.name" v-if="JSON.stringify(pramsObj) !== '{}'">
                         <template v-if="pramsObj.type === 'isSelect'">
                              <a-select v-model="pramsObj.value" allowClear
                                        style="margin: -5px 0; width: 280px" :placeholder="'请选择'+pramsObj.name"
                                        :getPopupContainer="trigger => trigger.parentNode">
                        <a-select-option v-for="item in checkParamsList" :key="item.value" :value="item.value">{{ item.name }}</a-select-option>
                    </a-select>
                         </template>
                        <a-input-number v-else
                                        placeholder="请输入数值"
                                        :min="1"
                                        :max="99999999"
                                        :formatter="verify.limitTwoDecimals"
                                        :parser="verify.limitTwoDecimals" style="width: 100%" v-model="pramsObj.value"
                        ></a-input-number><template v-if="pramsObj.unit !== ''">({{pramsObj.unit}})</template>
            </a-form-item>
                <a-form-item v-bind="formStyle" label=" " class="noLabelAfter">
                    <a-button type="primary" @click="sendCmdFun">
                                确定
                            </a-button>
                </a-form-item>
        </a-form>
            <div v-if="JSON.stringify(resultData) !== '{}'">
                <div style="width: 100%; color: #464d5b;background: #e9ebed; height: 35px; line-height: 35px; padding-left: 10px;">
                    控制命令数据参数值( {{progress}}% )
                </div>
                <div class="detailSetting" style="width: 95%; margin: 10px auto; color: #414141">
                    {{resultData}}
                </div>
            </div>
        </div>
        <div style="text-align: center;background: rgba(0,0,0,0.5);border-radius: 4px;width: 100%;height:100%;position: fixed;left: 0;top: 0;
        padding-top: 300px;z-index: 999999; font-size: 20px"
             v-if="showSpin">
            <a-spin :tip="spinTxt"/>
        </div>
    </div>
</template>

<script>
    import {mapState} from 'vuex';
    import verify from 'libs/verify';
    /*  import * as State from "libs/dictData";
   const leftPrams = State.dictData.params;*/
    export default {
        name: 'deviceSetting',
        data() {
            return {
                spinTxt: '请稍后...',
                form: this.$form.createForm(this),
                verify,
                showSpin: false,
                formStatus: true,
                activeKey: ['1'],
                firstSaveObj: '',
                formObj: {},
                sendParams: {
                    'tid': this.$commonVariable.toolListAll[0].tid,
                    'did': '',
                    'cmd_name': '',
                    'param':
                        {
                            'module': 1001,
                            'arg': 1
                        }
                },
                checkId: this.$commonVariable.checkItemALL[0].value,
                functionType: 'set',
                singerSwitch: false,
                toolListAll: this.$commonVariable.toolListAll,
                checkItemALL: this.$commonVariable.checkItemALL,
                commandListAll: this.$commonVariable.commandListAll,
                functionTypeList: this.$commonVariable.functionTypeList,

                leftGetData: [],
                instrumentData: [],
                timer: null,
                progress: 0,
                resultData: {},
                belongCommandListAll: [],
                pramsObj: {},
                checkParamsList: []
            };
        },
        computed: {
            ...mapState({
                formStyle: state => state.common.formStyle,  //表单label，input宽度
                formItemLayoutWithOutLabel: state => state.common.formItemLayoutWithOutLabel  //表单label，input宽度
            })
        },
        filters: {
            /*textFilter ( type ) {
                return leftPrams[ type ].name;
            },
            textUnitFilter ( type ) {
                return leftPrams[ type ].units;
            }*/
        },
        methods: {
            getSendParamsObjFun() {
                this.pramsObj = this.belongCommandListAll.filter(item => item.cmd_name === this.sendParams.cmd_name)[0]
                this.checkParamsList = []
                if (this.pramsObj.cmd_name === 'trig_src') {
                    this.checkParamsList = this.$commonVariable.trigSrcType
                } else if (this.pramsObj.cmd_name === 'ext_trig_type') {
                    this.checkParamsList = this.$commonVariable.extTrigTypeList
                } else if (this.pramsObj.cmd_name === 'ld_output') {
                    this.checkParamsList = this.$commonVariable.outputList
                } else if (this.pramsObj.cmd_name === 'power_unit') {
                    this.checkParamsList = this.$commonVariable.powerUnitList
                }
            },
            getCommandListAll() {
                let enName = this.toolListAll.filter(item => item.tid === this.sendParams.tid)[0].enName
                this.belongCommandListAll = this.commandListAll.filter(item => item.belong.includes(enName))
                this.pramsObj = {}
                this.checkParamsList = []
            },
            getToolStatusFun() {
                let _this = this
                this.showSpin = true
                _this.$post('/lab/queryStatus').then(res => {
                    if (res.code === 200) {
                        let state = res.data.state  //0启动态：设备刚启动，尚未正常工作 1可处理操作任务 2 不可接收新任务 3 异常态
                        if (state === 0) {
                            _this.spinTxt = '设备刚启动，请稍后'
                            _this.$message.warning('设备刚启动，請稍等')
                            _this.showSpin = true
                            setTimeout(() => {
                                _this.getToolStatusFun()
                            }, 3000)
                        } else if (state === 1) {
                            _this.$message.success('设备处于空闲或完成态，可处理操作任务')
                            _this.showSpin = false
                        } else if (state === 2) {
                            /*_this.spinTxt = '设备存在正在处理的操作任务，不可接收新任务'
                            _this.showSpin = true*/
                            _this.showSpin = false
                        } else if (state === 3) {
                            _this.spinTxt = '设备处于某种，不可接收新任务'
                            _this.showSpin = true
                        }
                    } else {
                        _this.$message.error(res.msg)
                    }
                })
            },
            getToolCheckProgressFun() {
                let _this = this
                let prams = {
                    'tid': this.sendParams.tid,
                    'did': this.checkId
                }
                _this.$post('/lab/queryProgress', prams).then(res => {
                    if (res.code === 200) {
                        _this.progress = res.data.progress
                    } else {
                        _this.$message.error(res.msg)
                    }
                })
            },
            sendCmdFun() {
                let _this = this
                if(this.sendParams.tid=== undefined){
                    _this.$message.warning('请选择模块名称')
                    return
                }
                if(this.checkId === undefined){
                    _this.$message.warning('请选择检测项名称')
                    return
                }
                if(this.sendParams.cmd_name === undefined){
                    _this.$message.warning('请选择功能')
                    return
                }
                if(JSON.stringify(this.pramsObj) !== '{}' && (this.pramsObj.value === undefined || this.pramsObj.value === '' )){
                    _this.$message.warning('请选择'+this.pramsObj.name)
                    return
                }
                let data = this.pramsObj.value
                if ('bias' === this.sendParams.cmd_name) {
                    data = this.pramsObj.value * 100
                }
                let prams = {
                    'tid': this.sendParams.tid,
                    'did': this.checkId,
                    'cmd_name': this.functionType + '_' + this.sendParams.cmd_name,
                    'param':
                        {
                            'module': this.sendParams.tid,
                            'arg': data
                        }
                }
                _this.$post('/lab/sendCmd', prams).then(res => {
                    if (res.code === 200) {
                        let result = res.data.result
                        if (result === 0) {
                            _this.$message.success('设置数据成功')
                            _this.getToolCheckProgressFun()
                            _this.getResultFun()
                        } else if (result === 1) {
                            _this.$message.warning('请等待，数据查询中')
                            _this.getToolCheckProgressFun()
                        } else {
                            _this.$message.error('设置数据失败')
                        }
                    } else {
                        _this.$message.error(res.msg)
                    }
                })
            },
            /******** 进度查询得知测试工具已采集完数据后*********/
            getResultFun() {
                let _this = this
                let prams = {
                    'tid': this.sendParams.tid,
                    'did': this.checkId
                }
                _this.$post('/lab/getResult', prams).then(res => {
                    if (res.code === 200) {
                        _this.resultData = res.data
                    } else {
                        _this.$message.error(res.msg)
                    }
                })
            },
            /**
             * Function    : clearListTimer
             * Description : 关闭定时器
             * Input       : none
             * Return      : none
             */
            clearListTimer() {
                let _this = this;
                if (_this.timer !== null) {
                    clearInterval(_this.timer);
                    _this.timer = null;
                }
            }
        },
        created() {
            this.getToolStatusFun()
            this.getCommandListAll()
        }
    };
</script>
<style rel="stylesheet/less" lang="less">
.ant-spin-text {
    font-size: 25px !important;
}

.noLabelAfter {
    .ant-form-item-label label:after {
        content: ' ' !important;
    }
    button {
        margin-right: 20px
    }
}
</style>
